<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <meta name="format-detection" content="telephone=no" />
        <meta name="viewport" content="target-densityDpi=device-dpi, width=device-width, height=device-height, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no" />
        <!-- Bootstrap -->
    	<link href="../css/bootstrap.min.css" rel="stylesheet" media="screen">
    	<link href="../plugins/font-awesome/css/font-awesome.min.css" rel="stylesheet" media="screen">
    	<link rel="stylesheet" type="text/css" href="../css/index.css" />
    	<link rel="stylesheet" type="text/css" href="../css/style.css" />
		
        <title>Connecxion </title>
    </head>
	<style>
div.formulaire{
margin-top: 10em;
background-color: rgb(250, 250, 248);
padding: 1em;

}
button.btFacebook{
 
margin-top: 3em;
font-weight: bold;
color: white;
background-color: blue;
padding: 2em;
}
	</style>
    <body>
    	<div id="header">
		  <div>
	              <a class="dropdown-toggle" id="header-titre" role="button" data-toggle="dropdown" href="#"><img src="../img/logo3_small.png"></a>
		  </div>
		  
        </div>
        <div id="container-add"><button type="submit" class="btFacebook btn  btn-primary" onclick="login()">Facebook Connect</button></div>
		 <div id="container-add">
		<div class="formulaire">
			<legend></legend>
				<form method="post" action="">
				
				<input class="test" type="Email" id="email" name="email" placeholder="email "/>
				
				<td><input class="test" type="password" id="password" name="password" placeholder="mot de passe "/>
				
				
				<div>
				<button class="btn "  type="submit" id="saisirButton" name="saisirButton" style="color: black;padding: 0.5em;">connexion</button>
				</div> 
           <div >
           
           </div>
		</div>
		</div>
       
        
        
        <!-- <script type="text/javascript" src="../cordova-2.7.0.js"></script> -->
			<!-- cordova -->
			 <script type="text/javascript" src="../js/cordova-2.6.0.js"></script>
		 <!-- cordova facebook plugin -->
		<script src="../js/cdv-plugin-fb-connect.js"></script>
	    <!-- facebook js sdk -->
		<script src="../js/facebook-js-sdk.js"></script>
		<script type="text/javascript" src="../js/firebase.js"></script>
        <script type="text/javascript" src="../js/jquery.js"></script>
        <script type="text/javascript" src="../js/bootstrap-dropdown.js"></script>
        <script type="text/javascript" src="../js/md5.js"></script>
        <script type="text/javascript" src="../js/model.js"></script>
        <script>
           
            var friendIDs = [];
			var fdata;
			var usersFire = new Firebase("https://pricyme.firebaseio.com/users");
            var userData = "Test";
			
			function getLoginStatus() {
                FB.getLoginStatus(function(response) {
                                  if (response.status == 'connected') {
                                  alert('logged in');
                                  } else {
                                  alert('not logged in');
                                  }
                                  });
            }
			
            function me() {
                FB.api('/me/friends', { fields: 'id, name, picture' },  function(response) {
                       if (response.error) {
                       alert(JSON.stringify(response.error));
                       } else {
                       var data = document.getElementById('data');
					   fdata=response.data;
					   console.log("fdata: "+fdata);
                       response.data.forEach(function(item) {
                                             var d = document.createElement('div');
                                             d.innerHTML = "<img src="+item.picture+"/>"+item.name;
                                             data.appendChild(d);
                                             });
                       }
					var friends = response.data;
					console.log(friends.length); 
					for (var k = 0; k < friends.length && k < 200; k++) {
				        var friend = friends[k];
				        var index = 1;

				        friendIDs[k] = friend.id;
				        //friendsInfo[k] = friend;
					}
					console.log("friendId's: "+friendIDs);
                       });
            }
            
            function login() {
                FB.login(
                         function(responsed) {
	                         if (responsed.status == 'connected') {
	                        	 subscribeUser();
	                         } else {
	                         	alert('not logged in');
	                         }
                         },
                         { scope: "email,user_about_me,user_location" }
                         );
            }
            
            function subscribeUser(){
                // Set user
                var count;
                 usersFire.once('value', function(data) {
                     count = parseInt(data.val().count);

                     FB.api('/me?fields=currency,picture,first_name,last_name,email,bio,location', function(response) {
                         
                    	// Instanciation de l'objet
                    	 var user = new User();
                    	 user.username = response.username;
                    	 user.facebook_id = response.id;
                    	 user.first_name = response.first_name;
                    	 user.last_name = response.last_name; 
                    	 user.email = response.email;
                    	 user.about = response.bio;
                    	 user.adress = response.location.name;
                    	 user.currency = response.currency.user_currency;
                    	 user.picture = response.picture.data.url;
                    	 
                    	// Envoie du JSON au serveur
                    	 $.post("http://pricy.me/web/app_dev.php/api/user/new?json="+user.getJSON(), {})
                    	 .done(function(data) {

                    	   // On récupère le résultat
                    	   alert("Data Loaded: " + data);
                    	   writeToFile("user.txt",data);
                    	 })
                    	 .fail(function(data) { alert("error : "+ JSON.stringify(data)); })
                    	 .always(function() { alert("finished"); });
                         
                     });
                 
                });
                 
            }
            
            /* function getUserIdByEmail(email){
            	var emailsFire = usersFire.child('emails');
            	userFire.once('value', function(data) {
            		if(data.val())
            	});
            	alert(emailsFire.toString());
            }
			 */
            function writeToFile(file,data){
            	// Write to file
   			 window.requestFileSystem(LocalFileSystem.PERSISTENT, 0,
   				function(fileSystem) {
   	                fileSystem.root.getFile(file, {create: true, exclusive: false},
   	                function(fileEntry) {
   	                    fileEntry.createWriter(
	                    	function(writer) {
	                        	writer.write(data);
	                        }
   	                    , fail);
   	                }
   	                , fail);
   	            } 
   				, fail);
            }

            function readFromFile(file) {
                window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, 
                function(fileSystem) {
                    fileSystem.root.getFile(file, null,
                    function(fileEntry) {
                        fileEntry.file(
                        function(file){
                            var reader = new FileReader();
                            reader.onloadend = function(evt) {
                            	userData = evt.target.result;
                            };
                            reader.readAsText(file);
                        }
                        , fail);
                    }
                    , fail);
                }
                , fail);
            }

            
            function fail(error) {
                console.log(error.code);
            }
            
            
            document.addEventListener('deviceready', function() {
                                      try {
                                      //alert('Device is ready! Make sure you set your app_id below this alert.');
                                      FB.init({ appId: "373488886099532", nativeInterface: CDV.FB, useCachedDialogs: false });
                                      //document.getElementById('data').innerHTML = "";
                                      } catch (e) {
                                      alert(e);
                                      }
                 }, false);
            </script>
		
    </body>
</html>
